<template>
  <div>
    <el-menu
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
    >
      <el-menu-item index="5" style="margin-left:300px" v-if="flag2">登录</el-menu-item>
      <el-menu-item index="6" style="margin-left:-10px" v-if="flag2">注册</el-menu-item>
      <el-menu-item index="9" style="margin-left:300px" v-if="!flag2">欢迎您,{{this.$store.state.username}}</el-menu-item>
      <el-menu-item index="3"><i class="el-icon-shopping-cart-full"></i>购物车</el-menu-item>
      <el-menu-item index="7"><i class="el-icon-user-solid"></i>个人中心</el-menu-item>
    </el-menu>
    <search></search>
    <produces></produces>
  </div>
</template>

<script>
import Search from '../views/Search.vue'
import Produces from '../views/Produces.vue'
  export default {
    data() {
      return {
        activeIndex: '1',
        activeIndex2: '1',
        flag2:this.$store.state.flag2
      };
    },
    methods: {
      handleSelect(key, keyPath) {
        switch(key){
          case '5':this.$router.push('/login');
                    break;
          case '6':this.$router.push('/register');
                    break;
          case '3':
                    this.axios.post("/api/shopping/getUserGoods.php",{'userid':this.$store.state.userID}).then(res=>{
                    this.$store.dispatch('setUserGoodsinfo',res.data);
                    console.log(res.data);
                    this.$router.push('/shopcar');
                    });
                    break;
          case '7':
                     if(this.$store.state.username==""){
                      this.$router.push('/login');
                    } else{
                    let userName = {
                      'username':this.$store.state.username
                    }
                    this.axios.post("/api/shopping/getMarket.php",userName).then((res)=>{
                        if(res.data=="notLogin"){
                          this.$router.push('/login');
                        }else{
                          this.$store.dispatch('setMarketname',res.data.marketName);
                          this.$store.dispatch('setMarkettype',res.data.marketType);
                          this.$store.dispatch('setMarketauthor',res.data.marketAuthor);
                          this.$store.dispatch('setMarketemail',res.data.marketEmail);
                          this.$store.dispatch('setMarketphone',res.data.marketPhone);
                          console.log(res);
                          console.log(this.$store.state.marketName);
                        }
                    });
                    this.$router.push('/personinfo');
                    }
                    break;
          case '9':
          if(this.$store.state.username==""){
                      this.$router.push('/login');
                    } else{
                    let userName = {
                      'username':this.$store.state.username
                    }
                    this.axios.post("/api/shopping/getMarket.php",userName).then((res)=>{
                        if(res.data=="notLogin"){
                          this.$router.push('/login');
                        }else{
                          this.$store.dispatch('setMarketname',res.data.marketName);
                          this.$store.dispatch('setMarkettype',res.data.marketType);
                          this.$store.dispatch('setMarketauthor',res.data.marketAuthor);
                          this.$store.dispatch('setMarketemail',res.data.marketEmail);
                          this.$store.dispatch('setMarketphone',res.data.marketPhone);
                          console.log(res);
                          console.log(this.$store.state.marketName);
                        }
                    });  
                        this.$router.push('/personinfo');
          }
          break;
        }
        console.log(key,keyPath);
      }
    },
    components:{
        Search:Search,
        Produces:Produces
    }
  }
</script>